<template>
  <app-list>
    <template #search>
      <el-form inline class="gendansearch_form">
        <el-form-item>
          <el-input v-model="pageData.query.task_name" placeholder="任务名称" style="width: 200px" />
          <!-- <el-select v-model="pageData.query.task_name" placeholder="任务名称" style="width: 200px">
            <el-option v-for="item in []" :key="item.value" :label="item.label" :value="item.value" />
          </el-select> -->
        </el-form-item>
        <el-form-item>
          <el-input v-model="pageData.query.task_code" placeholder="任务编码" style="width: 200px" />
          <!-- <el-select v-model="pageData.query.task_code" placeholder="任务编码" style="width: 200px">
            <el-option v-for="item in []" :key="item.value" :label="item.label" :value="item.value" />
          </el-select> -->
        </el-form-item>
        <el-form-item>
          <el-select
            v-if="taskoptions.task_status"
            v-model="pageData.query.task_status"
            placeholder="任务状态"
            style="width: 200px"
          >
            <el-option
              v-for="item in taskoptions.task_status.options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" class="hovepcvicecolor" @click="pageActions.handleSearch">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button @click="clickreset()">重置</el-button>
        </el-form-item>
      </el-form>
    </template>
    <template #action>
      <el-button type="primary" class="hovepcvicecolor" icon="plus" @click="handleadd()">新增</el-button>
    </template>
    <el-table stripe :data="pageData.list" v-loading="pageData.loading">
      <el-table-column label="任务名称" prop="name" show-overflow-tooltip>
        <template #default="scope">
          <div>{{ scope.row.task_form.task_name }}</div>
        </template>
      </el-table-column>
      <el-table-column label="任务编码" prop="task_code" show-overflow-tooltip />
      <el-table-column label="创建时间" prop="add_date" show-overflow-tooltip />
      <el-table-column label="更新时间" prop="update_date" show-overflow-tooltip />
      <!-- <el-table-column label="账号状态" prop="status" show-overflow-tooltip /> -->
      <el-table-column label="开启/关闭" width="100" prop="status" show-overflow-tooltip>
        <template #default="scope">
          <el-switch
            v-model="scope.row.status"
            :active-value="1"
            :inactive-value="0"
            class="ml-2"
            @change="changeswitch(scope.row)"
          />
        </template>
      </el-table-column>
      <el-table-column label="操作" width="120">
        <template #default="scope">
          <div style="display: flex">
            <div class="csspointer" @click="handleEdit(scope.row)">编辑</div>
            <div class="csspointer" style="margin-left: 12px" @click.native.prevent="deleteRow(scope.row)">
              删除
            </div>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <template #footerRight>
      <el-pagination
        @size-change="pageActions.handleSizeChange"
        @current-change="pageActions.handlePageChange"
        v-model:current-page="pageData.page"
        v-model:page-size="pageData.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="pageData.total"
        background
      />
    </template>

    <GendanAdd v-model="showgendanadd.show" :addData="showgendanadd" @change="clickreset()" />
  </app-list>
</template>
  
  <script setup>
import { http } from '@/utils/http'
import { ElMessage, ElMessageBox } from 'element-plus'
import config from '@/config'
import { onMounted } from 'vue'
import GendanAdd from "./GendanAdd.vue";

const { pageData, pageActions, dialog } = usePage(
  () => {
    const query = {
      page: pageData.page,
      page_size: pageData.pageSize,
      task_type: 'robot_copy_task',
    }
    if (pageData.query.task_name != '') {
      query.task_name = pageData.query.task_name
    }
    if (pageData.query.task_code != '') {
      query.task_code = pageData.query.task_code
    }
    query.task_status = pageData.query.task_status

    return http.post('/robot/robot_list', query)
  },
  {
    query: {
      task_name: '',
      task_code: '',
      task_status: 1,
    },
  }
)

const showgendanadd = reactive({
  show: false,
  title: '新增跟单机器人',
  data: {},
})
const timetrans = inject('timetrans')
const task_name = ref('')
const taskoptions = ref({})

// 新增
const handleadd = () => {
  showgendanadd.show = true
  showgendanadd.stitle = '新增跟单机器人'
  showgendanadd.data = {}
}

// 编辑
const handleEdit = row => {
  showgendanadd.show = true
  showgendanadd.stitle = '编辑跟单机器人'
  showgendanadd.data = row

  console.log(showgendanadd)
}

// 删除
const deleteRow = row => {
  ElMessageBox.confirm('是否删除?', '提示', {
    confirmButtonText: '确认',
    cancelButtonText: '取消',
    type: 'warning',
  })
    .then(() => {
      http.delete(`/robot/robot_delete?task_code=${row.task_code}`).then(res => {
        ElMessage.success('删除成功')
        pageActions.refresh(true)
      })
    })
    .catch(() => {
      ElMessage({
        type: 'info',
        message: '已取消',
      })
    })
}
const clickreset = () => {
  pageData.query = {
    task_name: '',
    task_code: '',
    task_status: -1,
  }
  pageActions.refresh(true)
}
const changeform = () => {
  pageActions.refresh(true)
}
// 改变开启状态
const changeswitch = row => {
  const onSuccess = () => {
    ElMessage.success('操作成功')
    changeform()
  }
  http({
    method: 'post',
    url: '/robot/robot_status',
    data: row,
  }).then(onSuccess)
}
onMounted(() => {
    http.get(`/robot/robot_options`).then(res => {
      if (res.code == 200) {
        taskoptions.value = res.data.filter_options
      }
    })
})
</script>
  
  <style lang="scss" scoped>
</style>

<style>
.gendansearch_form .el-form-item {
  margin-bottom: 0;
  margin-right: 16px;
}
.gendansearch_form .el-select__selected-item{
  font-weight: 0;
}
</style>